关于wordpress使用rewrite规则后站点无法更新ssl证书的问题

语言编程

wordpress-logo-stacked-rgb.png

貌似 2017 年 1 月 1 日起是 http 使用的最后期限,所有站点都纷纷开始使用 https 访问。我最近也使用了acme.sh进行相关站点的 https 证书处理。但是唯独某个使用 wordpress 的站点老死无法生成证书,通过 acme 的 debug 模式可以看到服务器返回的是403 访问拒绝的错误。

通过更换虚拟目录和手工配置验证文件等方法纠错依旧无解。我的主机配置是 nginx 的 PHP 方案。后来想了一下是否是 nginx 配置文件有问题。最终在看wordpress 官方的 nginx rewrite时发现了问题的端倪。原来在 Global restrictions file 就是官方推荐的 rewrite 方案文件 wordpress.conf 其中有一段:

# Deny all attempts to access hidden files such as.htaccess,.htpasswd,.DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
    deny all;
}

因为ssl证书验证时候创建过程是要通过web方式访问站点根目录中的.well-known目录中生成验证字符串文件。因此这行rewrite导致该目录无法被验证服务访问。但是奇怪的是通过手工创建的文件可以通过http访问(可能是我当时幻觉了╥﹏╥...)。

解决问题的方法很简单,就是将nginx配置文件目录中的wordpress.conf里面找到上面那几行注释掉即可。

具体的设置可以参考hrwhisper的配置方法进行配置Nginx服务器,本机使用的配置文件是这样的,因为多个站点共用了证书生成路径,因此统一写成了ssl-key.conf文件进行配置。ssl.conf文件请详细查看这里
nginx站点的vhost配置文件

server {
        listen 443 ssl;
        server_name www.ezo.biz ezo.biz; charset utf-8; index index.html index.htm index.php; root /home/ccchen/www/ezo.biz; ssl_certificate /etc/letsencrypt/live/ezo.biz/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/ezo.biz/privkey.pem;
        include global/ssl.conf;

        if (!-e $request_filename) { rewrite ^(.\.php(\/.$ { #root html; fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php; fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params;
        }

}
server {
    listen  80;
    server_name ezo.biz www.ezo.biz; include global/ssl-key.conf; } 

ssl-key.conf文件

location ^~ /.well-known/acme-challenge/ { default_type "text/plain";
    root /home/ccchen/www/key;
}

location / {
    return 301 https://$server_name$request_uri;
}

BTW:如果日后使用acme.sh进行证书更新的时候切记使用下面的范例进行操作(注意路径):

acme.sh  --issue  -d mydomain.com -d www.mydomain.com  --webroot  /home/ccchen/www/key

其实/home/ccchen/www/key这个路径是所有站点都共用的,你可以自定义自己的路径,记得将改文件夹权限改为可读写。

如果你想独立管理每个站点的ssl证书的话,推荐查看这个文章,有介绍nginx 1.6+后版本的ssl配置。

-EOF-

添加新评论